package Server;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.util.Duration;


public class EierlaufController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Label timeBlueLabel;

    @FXML
    private Label timeRedLabel;
    
    private Timeline blueTimeline;
    private Timeline redTimeline;


    @FXML
    void initialize() {
    	
    }

	public void setTimes(String blueTime, String redTime) {
		timeBlueLabel.setText(blueTime);
		timeRedLabel.setText(redTime);
	}
	
	public void startBlueTimer() {
		if (blueTimeline != null) {
			if(blueTimeline.getStatus().equals(blueTimeline.getStatus().STOPPED)){
				blueTimeline.playFromStart();
			}else{
				blueTimeline.stop();
			}
		}else{
			
			timeBlueLabel.setText("0");
			blueTimeline = new Timeline();
			blueTimeline.setCycleCount(Timeline.INDEFINITE);
			blueTimeline.getKeyFrames().add(
					new KeyFrame(Duration.millis(10),
							new EventHandler<ActionEvent>() {
						int seconds = 0;
						
						// KeyFrame event handler
						public void handle(ActionEvent event) {
							seconds++;
							// update timerLabel
							timeBlueLabel.setText("" + seconds);
						}
					}));
			blueTimeline.playFromStart();
		}
	}

	public void startRedTimer(){
		if (redTimeline != null) {
			if(redTimeline.getStatus().equals(redTimeline.getStatus().STOPPED)){
				redTimeline.playFromStart();
			}else{
				redTimeline.stop();
			}
    		}else{

    			timeRedLabel.setText("0");
    			redTimeline = new Timeline();
    			redTimeline.setCycleCount(Timeline.INDEFINITE);
    			redTimeline.getKeyFrames().add(
    					new KeyFrame(Duration.millis(10),
    							new EventHandler<ActionEvent>() {
    								int seconds = 0;

    								// KeyFrame event handler
    								public void handle(ActionEvent event) {
    									seconds++;
    									// update timerLabel
    									timeRedLabel.setText("" + seconds);
    								}
    			}));
    			redTimeline.playFromStart();
    		}
    }

}
